package cn.eveo.refreshandloadmore.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.eveo.refreshandloadmore.R;

/**
 * Created by Administrator on 2016/9/12.
 */
public class RecyclerAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List<String> datas;


    public RecyclerAdapter(Context mContext, List<String> datas) {
        this.datas = datas;
        this.mContext = mContext;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_adapter, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;

        viewHolder.itemView.setTag(datas.get(position));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (null == onItemClickListener) return;
                onItemClickListener.onClick(v);
            }
        });

        viewHolder.textView.setText(datas.get(position));
        viewHolder.textView.setHeight(100 + (position % 3) * 30);
    }

    @Override
    public int getItemCount() {
        return null == datas ? 0 : datas.size();
    }


    private static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            textView= (TextView) itemView.findViewById(R.id.item_adapter_title);
        }
    }

    interface OnItemClickListener {
        void onClick(View v);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}